#ifndef __SPI_FLASH_H__
#define __SPI_FLASH_H__

#include "gd32f3x0.h"

#ifdef __cplusplus
extern "C" {
#endif

#define SPI_FLASH_CH            SPI0
#define SPI_FLASH_SIZE          0x400000
#define SPI_FLASH_PageSize      0x100
#define SPI_FLASH_SectorSize    0x1000
#define SPI_FLASH_SECTORS       (SPI_FLASH_SIZE / SPI_FLASH_SectorSize)
#define SPI_FLASH_CS_LOW()      GPIO_BC(GPIOA) = GPIO_PIN_4
#define SPI_FLASH_CS_HIGH()     GPIO_BOP(GPIOA) = GPIO_PIN_4

uint8_t     SPI_FLASH_SendByte(uint8_t byte);
uint8_t     SPI_FLASH_ReadByte(void);
uint16_t    SPI_FLASH_SendHalfWord(uint16_t HalfWord);
void        SPI_FLASH_WriteEnable(void);
void        SPI_FLASH_WaitForWriteEnd(void);
uint32_t    SPI_FLASH_ReadID(void);
void        SPI_FLASH_SectorErase(uint32_t SectorAddr);
void        SPI_FLASH_SectorsErase(uint32_t SectorAddr, uint32_t SectorNum);
void        SPI_FLASH_BulkErase(void);
void        SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void        SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void        SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead);
void        SPI_FLASH_StartReadSequence(uint32_t ReadAddr);

#ifdef __cplusplus
}
#endif

#endif
